package com.pucheng.bracelet.ui.fragment.steps

import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.Configure
import com.pucheng.bracelet.app.util.DatetimeUtil
import com.pucheng.bracelet.data.bean.StepsBean
import com.pucheng.bracelet.viewmodel.steps.StepsToDayViewModel
import com.pucheng.bracelet.viewmodel.steps.StepsViewModel
import kotlinx.android.synthetic.main.fragment_steps_to_week.*
import me.hgj.jetpackmvvm.base.fragment.BaseVmFragment

class StepsToWeekFragment : BaseVmFragment<StepsToDayViewModel>() {
    private val stepsViewModel by activityViewModels<StepsViewModel>()

    private lateinit var stepsToWeekAdp: BaseQuickAdapter<StepsBean, BaseViewHolder>

    override fun createObserver() {
        stepsViewModel.apply {
            weekStepsData.observe(viewLifecycleOwner, Observer {
                if (!it.isNullOrEmpty()) {
                    stepsToWeekAdp = object :
                        BaseQuickAdapter<StepsBean, BaseViewHolder>(
                            R.layout.item_week_data,
                            it as MutableList<StepsBean>?
                        ) {
                        override fun convert(holder: BaseViewHolder, item: StepsBean) {
                            holder.getView<LinearLayout>(R.id.lin_week).layoutParams = ViewGroup.LayoutParams(Configure.screenWidth / 7 ,ViewGroup.LayoutParams.MATCH_PARENT )
                            var pg = holder.getView<ProgressBar>(R.id.pg_week_data)
                            pg.max = stepsTager.value!!
                            pg.progress = item.stepsNum.toInt()
                            pg.progressDrawable = resources.getDrawable(R.drawable.steps_progress_vertical_white)
                            holder.setTextColorRes(R.id.tv_week, R.color.white)
                            holder.setText(
                                R.id.tv_week,
                                DatetimeUtil.formatDate(
                                    DatetimeUtil.getCustomTime(item.date),
                                    DatetimeUtil.DATE_EE
                                )
                            )
                        }

                    }
                    rv_toweek.adapter = stepsToWeekAdp
                }
            })
        }
    }

    override fun dismissLoading() {
    }

    override fun initView(savedInstanceState: Bundle?) {
        Configure.init(activity)
        val layoutManager = LinearLayoutManager(activity)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        rv_toweek.layoutManager = layoutManager
    }

    override fun layoutId(): Int = R.layout.fragment_steps_to_week

    override fun lazyLoadData() {
    }

    override fun showLoading(message: String) {
    }

    companion object {
        fun newInstance(): StepsToWeekFragment {
            val fragment = StepsToWeekFragment()
            return fragment
        }
    }
}